package com.zkingsoft.actions.common;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.xmlbeans.impl.xb.xmlconfig.Extensionconfig.Interface;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STUnsignedDecimalNumber;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sun.tools.doclets.formats.html.resources.standard;
import com.sun.tools.javac.resources.compiler;
import com.zkingsoft.model.Admin;
import com.zkingsoft.model.Coach;
import com.zkingsoft.model.Comment;
import com.zkingsoft.model.Grade;
import com.zkingsoft.model.PaginationVO;
import com.zkingsoft.model.Student;
import com.zkingsoft.model.StudentCoach;
import com.zkingsoft.model.Subject;
import com.zkingsoft.model.User;
import com.zkingsoft.services.AdminService;
import com.zkingsoft.services.CoachService;
import com.zkingsoft.services.CoachSubjectService;
import com.zkingsoft.services.CommentService;
import com.zkingsoft.services.GradeService;
import com.zkingsoft.services.StudentCoachService;
import com.zkingsoft.services.StudentService;
import com.zkingsoft.services.SubjectService;

/**
 * @project Fusion-ERP
 * @package com.fusionzoom.erp.config
 * @class LetterConfigController.java
 * @author Matrix-J E-mail:<935090232@qq.com>
 * @date 2015年9月29日10:31
 * @description 站内信配置
 */
@Controller
public class CommonController extends BaseController {

	private static final Logger log = Logger.getLogger(CommonController.class);

	@Resource
	private CoachService coachService;

	@Resource
	private StudentService studentService;

	@Resource
	private SubjectService subjectService;

	@Resource
	private AdminService adminService;

	@Resource
	private StudentCoachService studentCoachService;

	@Resource
	private CoachSubjectService coachSubjectService;

	@Resource
	private GradeService gradeService;

	@Resource
	private CommentService commentService;

	@RequestMapping(value = "/regist")
	public String messagelistiIn(HttpServletRequest request) {

		log.info("进入报名页面...");

		List<Coach> coachList = coachService.findInPage(null, null);
		request.setAttribute("coachList", coachList);

		List<Subject> subjectList = subjectService.findInPage(null, null);
		request.setAttribute("subjectList", subjectList);

		return "regist";

	}

	@RequestMapping(value = "/doregist")
	public String doregist(HttpServletRequest request, Student student) {
		log.info("开始注册...");

		// 账号去重
		Student temp = new Student();
		temp.setAccount(student.getAccount());
		List<Student> list = studentService.findByModel(temp);
		if (list != null && list.size() > 0) {
			request.setAttribute("type", "err");
			request.setAttribute("msg", "账号 【" + student.getAccount() + "】:已经被注册！");
			request.setAttribute("url", "regist");
			return "msg";
		}
		studentService.add(student);
		StudentCoach sc = new StudentCoach();
		sc.setStudentId(student.getId());
		sc.setCoachId(student.getCoach().getId());
		sc.setSubjectName(student.getSubjectCate());
		// 添加学生教练中间表
		studentCoachService.add(sc);

		// 添加学生成绩信息
		String[] subjectName = { "科目一", "科目二", "科目三", "科目四" };
		for (int i = 0; i < subjectName.length; i++) {
			Grade grade = new Grade();
			grade.setStudentId(student.getId());
			grade.setSubjectName(subjectName[i]);
			grade.setSubjectCate(student.getSubjectCate());
			gradeService.add(grade);
		}
		request.setAttribute("type", "ok");
		request.setAttribute("msg", "报名成功！快去登录吧");
		request.setAttribute("url", "gologin?type=1");
		return "msg";
	}

	@RequestMapping(value = "/gologin")
	public String gologin(HttpServletRequest request , int type) {
		log.info("进入登录页面...");
		request.setAttribute("type", type);
		return "login";
	}

	@RequestMapping(value = "/dologin")
	public String dologin(HttpServletRequest request, Student student) {
		log.info("开始登录...");
		List<?> list;
		if (student.getType() == 1) {
			list = studentService.findByModel(student);
		} else if (student.getType() == 2) {
			Coach coach = new Coach();
			coach.setAccount(student.getAccount());
			coach.setPassword(student.getPassword());
			list = coachService.findByModel(coach);
		} else if (student.getType() == 3) {
			Admin admin = new Admin();
			admin.setAccount(student.getAccount());
			admin.setPassword(student.getPassword());
			list = adminService.findByModel(admin);
		} else {
			request.setAttribute("err", "请选择登录类型");
			return "login";
		}

		if (list == null || list.size() < 1) {
			request.setAttribute("err", "账号或密码错误");
			return "login";
		}
		User user = (User) list.get(0);
		user.setType(student.getType());
		request.getSession().setAttribute("user", list.get(0));
		return "adminIndex";
	}

	@RequestMapping(value = "/exit")
	public String exit(HttpServletRequest request) {
		request.getSession().invalidate();
		return "login";
	}

	@RequestMapping(value = "/welcome")
	public String welcome() {
		return "welcome2";
	}

	@RequestMapping(value = "/editStudent")
	public String editStudent(HttpServletRequest request) {
		Student user = (Student) request.getSession().getAttribute("user");
		StudentCoach studentCoach = new StudentCoach();
		studentCoach.setStudentId(user.getId());
		List<StudentCoach> list = studentCoachService.findInPage(studentCoach, null);
		Coach coach = coachService.findById(list.get(0).getCoachId());
		request.setAttribute("coach", coach);
		return "editStudent";
	}

	@RequestMapping(value = "/doeditStudent")
	public String doeditStudent(HttpServletRequest request, Student student) {
		log.info("开始更新student...");
		Student user = (Student) request.getSession().getAttribute("user");
		student.setId(user.getId());
		studentService.modify(student);
		request.getSession().setAttribute("user", studentService.findById(user.getId()));
		request.setAttribute("type", "ok");
		request.setAttribute("msg", "信息修改成功");
		request.setAttribute("url", "editStudent");
		return "msg";
	}

	@RequestMapping(value = "/goStudentGrade")
	public String goStudentGrade(HttpServletRequest request) {
		Student user = (Student) request.getSession().getAttribute("user");
		Grade grade = new Grade();
		grade.setStudentId(user.getId());
		List<Grade> gradeList = gradeService.findInPage(grade, null);
		request.setAttribute("gradeList", gradeList);
		return "studentGrade";
	}

	@RequestMapping(value = "/goCommentCoach")
	public String goCommentCoach(HttpServletRequest request) {
		log.info("教练评价界面...");
		Student user = (Student) request.getSession().getAttribute("user");
		StudentCoach studentCoach = new StudentCoach();
		studentCoach.setStudentId(user.getId());
		List<StudentCoach> list = studentCoachService.findInPage(studentCoach, null);
		Coach coach = coachService.findById(list.get(0).getCoachId());
		Comment comment = new Comment();
		comment.setStudentId(user.getId());
		List<Comment> commentList = commentService.findInPage(comment, null);
		if (commentList != null && commentList.size() > 0) {
			comment = commentList.get(0);
		}
		request.setAttribute("comment", comment);
		request.setAttribute("coach", coach);
		return "commentCoach";
	}

	@RequestMapping(value = "/doCommentCoach")
	public String doCommentCoach(HttpServletRequest request, Comment comment) {
		log.info("添加教练评价...");

		comment.setTime(new Date());
		List<Comment> commentList = commentService.findInPage(comment, null);
		if (commentList != null && commentList.size() > 0) {
			Comment temp = commentList.get(0);
			comment.setId(temp.getId());
			commentService.modify(comment);
		} else {
			commentService.add(comment);
		}
		request.setAttribute("type", "ok");
		request.setAttribute("msg", "感谢您的评价，我们会做的更好！");
		request.setAttribute("url", "goCommentCoach");
		return "msg";
	}

	/*********************
	 * 教练部分
	 ******************************/

	@RequestMapping(value = "/goLookComment")
	public String goLookComment(HttpServletRequest request, Student student, PaginationVO pageVo) {
		log.info("教练评价列表...");
		User user = (User) request.getSession().getAttribute("user");
		if (pageVo == null) {
			pageVo = new PaginationVO();
		}
		student.setCoachId(user.getId());
		List<Student> studentList = studentService.findInPage(student, pageVo);
		pageVo.setTotalRecold(studentService.findTotal(student));
		for (Student student2 : studentList) {
			Grade grade = new Grade();
			grade.setStudentId(student2.getId());
			student2.setGrades(gradeService.findInPage(grade, null));
		}
		request.setAttribute("studentList", studentList);
		request.setAttribute("pageVo", pageVo);
		request.setAttribute("student", student);
		return "/lookComment";
	}

	@RequestMapping(value = "/goStudentList")
	public String goStudentList(HttpServletRequest request, Student student, PaginationVO pageVo) {
		log.info("学员列表...");
		if (pageVo == null) {
			pageVo = new PaginationVO();
		}
		List<Student> studentList = studentService.findInPage(student, pageVo);
		pageVo.setTotalRecold((studentService.findTotal(student)));
		request.setAttribute("pageVo", pageVo);
		request.setAttribute("student", student);
		request.setAttribute("studentList", studentList);
		return "/studentList";
	}

	@RequestMapping(value = "/delStudent")
	public String delStudent(HttpServletRequest request, int id) {
		log.info("删除学员...");
		studentService.removeById(id);
		return "redirect:/goStudentList";
	}

	@RequestMapping(value = "/goEditGrade")
	public String goEditGrade(HttpServletRequest request, int id) {
		log.info("设置学员成绩...");
		Student student = studentService.findById(id);

		Grade grade = new Grade();
		grade.setStudentId(id);
		List<Grade> gradeList = gradeService.findInPage(grade, null);
		request.setAttribute("gradeList", gradeList);
		request.setAttribute("student", student);
		return "editGrade";
	}

	@RequestMapping(value = "/doEditGrade")
	public String doEditGrade(HttpServletRequest request, Student student) {
		log.info("设置学员成绩...");

		List<Grade> grades = student.getGrades();
		for (Grade grade : grades) {
			gradeService.modify(grade);
		}
		request.setAttribute("type", "ok");
		request.setAttribute("msg", "设置成功");
		request.setAttribute("url", "goStudentList");
		return "msg";
	}

	@RequestMapping(value = "/goCoachList")
	public String goCoachList(HttpServletRequest request, Coach coach, PaginationVO pageVo) {
		log.info("教练列表...");
		if (pageVo == null) {
			pageVo = new PaginationVO();
		}
		List<Coach> coachList = coachService.findInPage(coach, pageVo);
		pageVo.setTotalRecold((coachService.findTotal(coach)));
		request.setAttribute("pageVo", pageVo);
		request.setAttribute("coach", coach);
		request.setAttribute("coachList", coachList);
		return "coachList";
	}

	@RequestMapping(value = "/delCoach")
	public String delCoach(HttpServletRequest request, int id) {
		log.info("删除教练...");
		coachService.removeById(id);
		request.setAttribute("type", "ok");
		request.setAttribute("msg", "删除成功");
		request.setAttribute("url", "goCoachList");
		return "msg";
	}

	@RequestMapping(value = "/goEditCoach")
	public String goEditCoach(HttpServletRequest request, int id) {
		log.info("编辑教练信息...");
		Coach coach = coachService.findById(id);
		request.setAttribute("user", coach);
		return "editCoach";
	}

	@RequestMapping(value = "/doeditCoach")
	public String doeditCoach(HttpServletRequest request, Coach coach) {
		log.info("开始修改教练信息...");
		coachService.modify(coach);
		request.setAttribute("type", "ok");
		request.setAttribute("msg", "修改成功");
		
		//如果是教练
		if(request.getSession().getAttribute("user").getClass().equals(Coach.class)){
			request.setAttribute("url", "goEditCoach?id="+coach.getId());
			return "msg";
		}else{
			request.setAttribute("url", "goCoachList");
			return "msg";
		}
		
	}

	@RequestMapping(value = "/goAddCoach")
	public String goAddCoach(HttpServletRequest request) {
		log.info("编辑教练信息...");
		return "addCoach";
	}

	@RequestMapping(value = "/doAddCoach")
	public String doAddCoach(HttpServletRequest request, Coach coach) {
		log.info("开始添加教练信息...");

		Coach Coach2 = new Coach();
		Coach2.setAccount(coach.getAccount());
		List<Coach> list = coachService.findByModel(Coach2);
		
		if (list != null && list.size() > 0) {
			request.setAttribute("type", "err");
			request.setAttribute("msg", "账号 【" + Coach2.getAccount() + "】:已经被注册！");
			request.setAttribute("url", "goAddCoach");
			return "msg";
		}
		
		coachService.add(coach);
		request.setAttribute("type", "ok");
		request.setAttribute("msg", "添加成功");
		request.setAttribute("url", "goCoachList");
		return "msg";

	}

}